package application.model 
{
	import application.ApplicationFacade;
	import application.model.VO.LayoutVO;
	import flash.geom.Rectangle;
	import ginger.ui.simple.SimpleLayout;
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	/**
	 * ...
	 * @author Ning
	 */
	public class LayoutProxy extends Proxy implements IProxy
	{
		public static const NAME:String = 'LayoutProxy';
		public function LayoutProxy() 
		{
			super(NAME, { } );
		}
		/**
		 * 设置配置
		 */
		public function setConfig():void
		{
			var config:XML = AppGlobal.RESOURES_POOL.getXML(AppGlobal.LAYOUT_KEY);
			var layoutXML:XMLList = config.layout;

			for (var i:int = 0; i < layoutXML.length(); i++ )
			{
				var layoutVO:LayoutVO = new LayoutVO;
				layoutVO.anchorType = getAnchorType(String(layoutXML[i].@['layout']));
				layoutVO.left = String(layoutXML[i].@['left'])==''?NaN:Number(layoutXML[i].@['left']);
				layoutVO.right = String(layoutXML[i].@['right'])==''?NaN:Number(layoutXML[i].@['right']);
				layoutVO.top =String(layoutXML[i].@['top'])==''?NaN:Number(layoutXML[i].@['top']);
				layoutVO.bottom = String(layoutXML[i].@['bottom'])==''?NaN:Number(layoutXML[i].@['bottom']);
				layoutVO.centerX = String(layoutXML[i].@['centerX'])==''?NaN:Number(layoutXML[i].@['centerX']);
				layoutVO.centerY = String(layoutXML[i].@['centerY']) == ''?NaN:Number(layoutXML[i].@['centerY']);
				if (String(layoutXML[i].@['layoutWidth']) != '' && String(layoutXML[i].@['layoutHeight'] != '')) {
					layoutVO.targetRect = new Rectangle(0, 0, Number(layoutXML[i].@['layoutWidth']), Number(layoutXML[i].@['layoutHeight']));
				}
				data[String(layoutXML[i].@['key'])] = layoutVO;
			}
			sendNotification(ApplicationFacade.UPDATE_LAYOUT_CONFIG, data);
		}
		
		public function getLayout(key:String):LayoutVO {
			return data[key];
		}
		private function getAnchorType(value:String):int
		{
			var type:int = 0;
			var arr:Array = value.split(',');
			for (var i:int = 0; i < arr.length; i++ )
			{
				switch(arr[i])
				{
					case 'L':
					type += SimpleLayout.LEFT;
					break;
					case 'R':
					type += SimpleLayout.RIGHT;
					break;
					case 'T':
					type += SimpleLayout.TOP;
					break;
					case 'B':
					type += SimpleLayout.BOTTOM;
					break;
				}
			}
			return type;
		}
	}
}